package cn.toguide.ffmpeg.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.util.Map;

@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "ffmpeg-sdk")
public class FFMpegSDKProperties implements InitializingBean {

    public static final String FFMPEG_PATH_REPLACE_KEY = "ffmpeg";

    public static final String FFPROBE_PATH_REPLACE_KEY = "ffprobe";

    public static final String INPUT_FILE_REPLACE_KEY = "inputPath";

    public static final String OUTPUT_FILE_REPLACE_KEY = "outputPath";

    /**
     * ffmpeg位置
     */
    private String ffmpegPath;

    /**
     * ffprobe位置
     */
    private String ffprobePath;

    /**
     * 转码后目标文件位置，为空则创建临时文件
     */
    private String swapPath;

    /**
     * 转码后输入输出文件是否删除，默认不删除
     */
    private boolean cleanup;

    private Map<String, String> commands;

    @Override
    public void afterPropertiesSet() {
        if (!StringUtils.hasText(ffmpegPath)) {
            setFFMpegPathBySystemEnv();
        }
        log.info("FFMpegPath: {}", ffmpegPath);
        log.info("FFProbePath: {}", ffprobePath);
        if (!StringUtils.hasText(swapPath)) {
            this.swapPath = getTempPathBySystem();
        }
        log.info("SwapPath: {}", swapPath);
        log.info("Cleanup: {}", cleanup);
        if (commands != null) {
            commands.forEach((k, v) -> {
                if (v != null) {
                    if (v.startsWith(FFMPEG_PATH_REPLACE_KEY)) {
                        String value = ffmpegPath + v.substring(FFMPEG_PATH_REPLACE_KEY.length());
                        commands.put(k, value);
                        log.info("{} -> {}", k, value);
                    }
                    if (v.startsWith(FFPROBE_PATH_REPLACE_KEY)){
                        String value = ffprobePath + v.substring(FFPROBE_PATH_REPLACE_KEY.length());
                        commands.put(k, value);
                        log.info("{} -> {}", k, value);
                    }
                }
            });
        }
    }

    private static String getTempPathBySystem() {
        String os = System.getProperty("os.name").toLowerCase();
        String tempDir = "";

        if (os.contains("win")) {
            tempDir = System.getenv("TEMP");
        } else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) {
            tempDir = System.getenv("TMPDIR");
        } else if (os.contains("mac")) {
            tempDir = System.getProperty("java.io.tmpdir");
        }
        return tempDir;
    }


    private void setFFMpegPathBySystemEnv() {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("win")) {
            String ffmpegHome = System.getenv("FFMPEG");
            if (!StringUtils.hasText(ffmpegHome)) {
                ffmpegHome = System.getenv("FFMPEG_HOME");
            }
            if (ffmpegHome != null) {
                ffmpegPath = ffmpegHome + "\\ffmpeg.exe";
                ffprobePath = ffmpegHome + "\\ffprobe.exe";
            }
        } else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) {
            String ffmpegHome = System.getenv("FFMPEG");
            if (!StringUtils.hasText(ffmpegHome)) {
                ffmpegHome = System.getenv("FFMPEG_HOME");
            }
            if (ffmpegHome != null) {
                ffmpegPath = ffmpegHome + "/ffmpeg";
                ffprobePath = ffmpegHome + "/ffprobe";
            }
        } else if (os.contains("mac")) {
            String ffmpegHome = System.getenv("FFMPEG");
            if (!StringUtils.hasText(ffmpegHome)) {
                ffmpegHome = System.getenv("FFMPEG_HOME");
            }
            if (ffmpegHome != null) {
                ffmpegPath = ffmpegHome + "/ffmpeg";
                ffprobePath = ffmpegHome + "/ffprobe";
            }
        }
    }

}
